<?php

namespace app\api\controller;

use app\common\controller\Api;

use think\Response;
use think\exception\HttpResponseException;
use think\Db;
/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['test', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2'];

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */

    //生成唯一用户标识id，长度36位，带"-"
    function create_guid()
    {
        $charid = strtolower(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = substr($charid, 0, 8) . $hyphen
            . substr($charid, 8, 4) . $hyphen
            . substr($charid, 12, 4) . $hyphen
            . substr($charid, 16, 4) . $hyphen
            . substr($charid, 20, 12);
        return $uuid;
    }

    // http://rhc.com/api/demo/test
    protected $databaseInfo = 'mysql://backend:backend1234@47.103.82.136:3306/zh_platform#utf8';
    public function test()
    {
        // 插入数据
        $point = [
            [121.09,31.78],
            [121.90,31.0013],
            [121.90,31.0055]
        ];
        $point_binpack = "";
        for($i = 0; $i < count($point); $i++)
        {
            $lon1 = pack("d*", $point[$i][0]);
            $lat1 = pack("d*", $point[$i][1]);
            $point_binpack .= $lon1 . $lat1;
        }
        $Points = unpack("d*", $point_binpack);
        echo json_encode($Points);
        $a0 = $point_binpack;
        $ID = $this->create_guid();
        $Name = $this->create_guid();
//        $point_binpack = strval($point_binpack);
//        $point_binpack = HEX($point_binpack);
        $sql = "INSERT INTO fence(`ID`, `Source`, `Name`, `Rule`, `StartTime`, `EndTime`, `PtNum`, `Points`, `Height`, `RecordTime`) ";
        $sql .= "VALUES ('{$ID}', '1', '{$Name}', 0, '00:00:00', '23:59:00', 3, :a, 100, '2021-02-04 14:57:39')";
        $bind = [
            'a' => $point_binpack,
        ];
        Db::connect($this->databaseInfo)->query($sql, $bind);
        # 读出测试
        $sql = "SELECT `Points` FROM fence WHERE `Name` = '{$Name}'";
        $list = Db::connect($this->databaseInfo)->query($sql);
        $list = $list[0];
        $a1 = $list["Points"];
        echo "<br>".$a0."<br>";
        echo $a1."<br>";
        $Points = unpack("d*", $list["Points"]);
        echo json_encode($Points);
        return;
        // $point = [];
        // $point[] = [121.09, 31.78];
        // $point[] = [121.90, 31.0013];
        // $point[] = [121.11, 31.00];
        // $this->success($point);
    }

    function success2($result)
    {
        $response = Response::create($result, 'json', 200)->header([]);
        throw new HttpResponseException($response);
    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

}
